//
//  CBJComposeView.swift
//  Weibo
//
//  Created by 崔健 on 16/5/28.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit

class CBJComposeView: UIView {
    
    
    var btns : [CBJComposeBtn] = [CBJComposeBtn]()
    
    
    
    override init(frame: CGRect) {
        
        
        super.init(frame: UIScreen.mainScreen().bounds)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(backImageView)
        
        backImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(self)
        }
        
        addchildBtns()
        
    }
    
    private func addchildBtns(){
        
        let count : Int = btnItems.count
        let btnWidth : CGFloat = 80
        let btnHeight : CGFloat = 120
        let btnMargin : CGFloat = (kScreenWidth - 3 * btnWidth) / 4
        
        for i in 0..<count{
            
            let btn : CBJComposeBtn = CBJComposeBtn()
            btn.composeItem = btnItems[i]
            let col : Int = i / 3
            let row : Int = i % 3
            let x : CGFloat = btnMargin + CGFloat(row) * (btnMargin + btnWidth)
            let y : CGFloat = btnMargin + CGFloat(col) * (btnMargin + btnHeight) + kScreenHeight
            btn.frame = CGRect(x: x, y: y, width: btnWidth, height: btnHeight)
            btns.append(btn)
            btn.addTarget(self, action: #selector(btnDidClick(_:)), forControlEvents: .TouchUpInside)
            addSubview(btn)
        }
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        for (index,btn) in btns.enumerate(){
        
            startPopAnimation(index, btn: btn, offset: 350)
        }
        
    }
    
    
    @objc private func btnDidClick(btn : CBJComposeBtn){
        
        for item in btns{
            
            var scale : CGFloat? = 0
            
            if item == btn {
                
                scale = 2.0
                
            }else{
               
                scale = 0.8
            }
            
            
            UIView.animateWithDuration(0.25, animations: {
                
                    item.alpha = 0
                    item.transform = CGAffineTransformMakeScale(scale!,scale!)
                
                }, completion: nil)
        }
        
        guard let targetStr = btn.composeItem?.target else{
        
            return
        }
        
        
        let vc = NSClassFromString(targetStr).self as! UIViewController.Type
        let navVc = UINavigationController(rootViewController: vc.init())
        // 跳转控制器
        self.target?.presentViewController(navVc, animated: true , completion: {
            
            self.removeFromSuperview()
        })

        
    }
    
    
    var target : UIViewController?
    func showTarget(target : UIViewController){
    
        self.target = target
        // 将目标控制器的view添加到当前视图上
        target.view.addSubview(self)
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        super.willMoveToSuperview(newSuperview)
        for (index , item) in btns.enumerate(){
            
            startPopAnimation(index, btn: item,offset: -350)
            
        }
        
    }
    
    func startPopAnimation(index : Int , btn : CBJComposeBtn , offset : CGFloat){
        
        UIView.animateWithDuration(0.8, delay: Double(index) * 0.25, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: {
            
                btn.center = CGPoint(x: btn.center.x, y: btn.center.y + offset)
            
            }) { (_) in
                
                if offset > 0 && index + 1  == self.btns.count{
                
                    self.removeFromSuperview()
                }
                
        }
        
    }
    
    private var backImageView : UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    
    private lazy var btnItems : [CBJCompose] = {
        
        var items = [CBJCompose]()
        
        let path : String = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)!
        
        let itemArray = NSArray(contentsOfFile: path) as! [[String : AnyObject]]
        
        for item in itemArray{
            
            items.append(CBJCompose(dict: item))
        }
        
        return items
    }()
}
